const express = require('express');
const router = express.Router();
const { verifyToken } = require('../../utils/jwt');
const sesstion = require("../../mysql/sessions")
router.use(sesstion);

router.use("/login", require("./login/login"));
router.use("/logout", require("./login/logout"));



router.use((req, res, next) => {
    if (req.url != "/login") {
        if (req.headers.authorization == undefined || req.headers.authorization == "") {
            res.send({ message: "未携带Authorization请求头" })
        } else {
            const token = String(req.headers.authorization.split(" ").pop());
            verifyToken(token).then(data => {
                next()
            }).catch(err => {
                try {
                    res.send(err)
                } catch (error) {
                    res.send(error)
                }
            })
        }

    }
})
router.get('/', (req, res) => {
    let http = `${req.protocol}://${req.headers.host}`;
    res.send({ http });

});
router.use("/home", require("./home/index"));
router.use("/userlist", require("./users/userlist"));
router.use("/userlog", require("./users/userlog"));
router.use("/power", require("./power/index"));
router.use("/third", require("./third/mail"));



module.exports = router;